package com.tgy.leetcode;

import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author: tgy
 * @Date: 12/28/20 9:58 AM
 *  给定由开始时间和结束时间组成的会议时间间隔数组，请返回至少要几间会议室。
 *
 *  如:
 *  [[0, 30],[5, 10],[15, 20]],
 *  返回 2
 */
public class _252_会议室_I_02 {

    public int minMeetingRooms(int[][] meetingRooms) {

        if (meetingRooms == null || meetingRooms.length == 0 || meetingRooms[0].length == 0) {

            return 0;
        }

        int rooms = 0;
        int len = meetingRooms.length;
        int[] firsts = new int[len];
        int[] seconds = new int[len];

        for (int i = 0; i < meetingRooms.length; i++) {

            firsts[i] =  meetingRooms[i][0];
            seconds[i] = meetingRooms[i][1];
        }

        Arrays.sort(firsts);
        Arrays.sort(seconds);


        //[[0, 30],[5, 10],[15, 20]],
        // 0,5,15
        // 10, 20,30

        // 5,15
        // 10,20
        int sIndex = 0;
        for (int i = 0; i < len; i++) {

//            firsts[i];

            if (firsts[i] < seconds[sIndex]) {
                // 新开一间
                rooms++;
            }else {

                // 使用旧的
                sIndex++;
            }
        }


       return rooms;
    }
}
